package ink.lovejinhu.leetcode;

import java.util.HashMap;

/**
 * @author jinhu
 * created 2021-06-29 8:51
 */
public class Solution_0168 {
    /**
     * 输入: 701=26*26+25=()*26+()*    ==    26*27
     * 输出: "ZY"
     * 有错
     *
     * @param columnNumber
     * @return
     */
    public String convertToTitle(int columnNumber) {
        HashMap<Integer, Character> map = new HashMap<>();
        for (int i = 0; i < 26; i++) {
            map.put(i + 1, (char) ('A' + i));

        }
        System.out.println(map);
        StringBuilder res = new StringBuilder();
        while (columnNumber > 26) {
            res.append(map.get(columnNumber % 26));
            columnNumber = columnNumber / 26;
        }
        res.append(map.get(columnNumber));
        return revereString(res.toString());
    }

    public String convertToTitle1(int columnNumber) {
        HashMap<Integer, Character> map = new HashMap<>();
        for (int i = 0; i < 26; i++) {
            map.put(i, (char) ('A' + i));
        }
        StringBuilder res = new StringBuilder();
        while (columnNumber>0){
         res.append(map.get((columnNumber-1)%26));
         columnNumber=(columnNumber-1)/26;
        }
        return revereString(res.toString());
    }

    public String revereString(String param) {
        StringBuilder stringBuilder = new StringBuilder();
        char[] chars = param.toCharArray();
        for (int i = chars.length - 1; i >= 0; i--) {
            stringBuilder.append(chars[i]);
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        Solution_0168 solution_0168 = new Solution_0168();
        System.out.println(solution_0168.convertToTitle1(701));
    }
}
